from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QLabel,
                             QPushButton, QHBoxLayout, QFrame, QScrollArea)
from PyQt5.QtCore import Qt, QTimer


class ReminderDialog(QDialog):
    def __init__(self, schedule: dict, parent=None):
        super().__init__(parent)
        self.setWindowTitle("🔔 日程提醒")
        icon = QIcon("E:/pythonwork/personal_schedule_system/rsc/icon.jpg")
        self.setWindowIcon(icon)
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.Dialog)
        self.setMinimumSize(400, 300)  # 不固定尺寸
        self.adjustSize()  # 自动调整
        self.setStyleSheet("""
                    QDialog {
                        background-color: qlineargradient(
                            spread:pad, x1:0, y1:0, x2:1, y2:1,
                            stop:0 #e0f7fa, stop:1 #ffffff
                        );
                        border-radius: 12px;
                        border: 1px solid #d5d8dc;
                    }
                    QScrollArea {
                        border: none;
                        background: transparent;
                    }
                """)
        self._init_ui(schedule)
        self._start_auto_close()

    def _init_ui(self, schedule: dict):
        layout = QVBoxLayout()
        layout.setContentsMargins(24, 24, 24, 24)  # 增加边距
        layout.setSpacing(8)

        # 标题
        title = QLabel(f"<b>{schedule.get('title', '无标题')}</b>")
        title.setFont(QFont("微软雅黑", 16, QFont.Bold))
        title.setStyleSheet("color: #2c3e50; margin-bottom: 8px;")
        layout.addWidget(title)

        # 时间信息
        time_layout = QVBoxLayout()

        start_time = schedule.get("start_time")
        end_time = schedule.get("end_time")

        start_label = QLabel(f"⏰ 开始:  {start_time.strftime('%m-%d %H:%M') if start_time else '无'}")
        start_label.setStyleSheet("color: #34495e;")
        start_label.setFont(QFont("微软雅黑", 10))

        end_label = QLabel(f"⏰ 结束:  {end_time.strftime('%m-%d %H:%M') if end_time else '无'}")
        end_label.setStyleSheet("color: #34495e;")
        end_label.setFont(QFont("微软雅黑", 10))

        time_layout.addWidget(start_label)
        time_layout.addWidget(end_label)

        layout.addLayout(time_layout)

        # 分隔线
        line = QFrame()
        line.setFrameShape(QFrame.HLine)
        line.setStyleSheet("border: 1px solid #eaecef;")
        layout.addWidget(line)

        # 备注（可滚动）
        scroll_area = QScrollArea()
        scroll_area.setMaximumHeight(150)  # 新增高度限制
        scroll_area.setWidgetResizable(True)

        notes = schedule.get("notes", "")
        note_text = notes if notes else "无备注信息"

        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)

        note_label = QLabel(f"📝 {note_text}")
        note_label.setWordWrap(True)
        note_label.setFont(QFont("微软雅黑", 11))
        note_label.setStyleSheet("padding: 8px; color: #566573; line-height: 150%;")
        scroll_area.setWidget(note_label)
        layout.addWidget(scroll_area)

        # 间隔
        layout.addSpacing(4)

        # 关闭按钮
        btn_layout = QHBoxLayout()
        btn_layout.addStretch()   # 弹性伸缩空间

        btn_close = QPushButton("知道了")
        btn_close.clicked.connect(self.accept)
        btn_close.setStyleSheet("""
                    QPushButton {
                        background-color: #3498db;
                        color: white;
                        padding: 8px 24px; 
                        border-radius: 6px;
                        font-size: 8pt;
                        font-weight: bold;
                        min-width: 80px;
                    }
                    QPushButton:hover {
                        background-color: #2980b9;
                     QPushButton:pressed {
                        background-color: #2472a4;
                    }
                """)
        btn_close.setCursor(Qt.PointingHandCursor)
        btn_layout.addWidget(btn_close)
        layout.addLayout(btn_layout)

        self.setLayout(layout)

    def _start_auto_close(self):
        """10秒后自动关闭"""
        QTimer.singleShot(10000, self.accept)